package com.x.wx.conf;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.x.wx.model.WxAccount;
import com.x.wx.rpc.WxAccountRpcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

@Component
public class InitConfig {


	private static final Logger logger = LoggerFactory.getLogger(InitConfig.class);

	@Reference
	private WxAccountRpcService wxAccountRpcService;

	@PostConstruct
	public void init() {
		logger.info("init WX CONFIG");
		//项目启动初始化
		WxAccount wxAccount = new WxAccount();
		wxAccount.setWxAccountState(1);
		List<WxAccount> wxAccounts = wxAccountRpcService.selectWxAccountList(wxAccount);
		ApiConfig ac;
		for(WxAccount account : wxAccounts){
			ac = new ApiConfig();
			// 配置微信 API 相关参数
			ac.setToken(account.getWxToken());
			ac.setAppId(account.getWxAppId());
			ac.setAppSecret(account.getWxAppSecret());
			/**
			 * 是否对消息进行加密，对应于微信平台的消息加解密方式： 1：true进行加密且必须配置 encodingAesKey
			 * 2：false采用明文模式，同时也支持混合模式
			 */
			ac.setEncryptMessage(account.getWxEncryptMessage());
			ac.setEncodingAesKey(account.getWxEncodingAesKey());
			ApiConfigKit.putApiConfig(ac);
		}
	}
}
